create table sjml.pass_rate_train_data
as
select t1.*,t2.name as product_name,t2.depart ,t2.rate_down ,t2.rate_upper ,t2.loan_cycle ,t2.refund_way ,t2.money_num
from
(select * from
(select row_number() over(partition by company_name order by busi_time desc) rn,company_name,
credit_line,money,
busi_time,product_id
from wechat_finance_db.chsell_order
where char_length(company_name)>1 and busi_time>=date_sub(CURRENT_DATE() ,interval 2 year)
) t where rn =1
) t1
join
wechat_finance_db.chsell_product t2
on t1.product_id=t2.id


create table sjml.pass_rate_train_data2
as
select t1.*,t2.money_num as money_num2
from sjml.pass_rate_train_data t1
left join
(select product_id,
GREATEST(max(credit_line),max(money/1000)) money_num
from sjml.pass_rate_train_data
group by product_id
having money_num is not null ) t2
on t1.product_id=t2.product_id

最后执行：
select
t1. *
from

(select tsca.*,
t2.product_id, t2.product_name, t2.depart, t2.rate_down, t2.rate_upper, t2.loan_cycle, t2.refund_way,
case when t2.money_num is not null then t2.money_num else cast(t2.money_num2 as Nullable(Float)) end as moeny_num,
                                                                                                        t2.credit_line, t2.money, t2.busi_time
from

(select *
 from
 tuoke_square_company_info
 where char_length(company_name) > 1
) tsca
join
(select *
from mysql(

'120.24.230.214', 'sjml', 'pass_rate_train_data2', 'root', 'SJ!2022@kyk') ) t2
on
tsca.company_name = t2.company_name
) t1

join

(select company_name, count( *)
num
from

(select tsca.*,
t2.product_id, t2.product_name, t2.depart, t2.rate_down, t2.rate_upper, t2.loan_cycle, t2.refund_way,
case when t2.money_num is not null then t2.money_num else cast(t2.money_num2 as Nullable(Float)) end as moeny_num,
                                                                                                        t2.credit_line, t2.money, t2.busi_time
from

(select *
 from
 tuoke_square_company_info
 where char_length(company_name) > 1
) tsca
join
(select *
from mysql(

'120.24.230.214', 'sjml', 'pass_rate_train_data2', 'root', 'SJ!2022@kyk') ) t2
on
tsca.company_name = t2.company_name
) t
group
by
company_name
having
num = 1
)  t2

on
t1.company_name = t2.company_name